/**
 * @file serial_device.cpp
 * @brief Code for XXX driver
 * @author He Shaohua (hsh15529870639@foxmail.com)
 * @date 2024-03-20
 * @version 1.0
 * @copyright Copyright (c) 2024, SNUT-Chaoyue Team
 * ****************************************************************************************************
 * @par Change Log:
 * <table>
 * <tr><th>Date         <th>Version       <th>Author      <th>Description
 * <tr><td>2024-03-20   <td> 1.0          <td>He Shaohua  <td>Creator
 *
 * </table>
 * ==================================----How to use this driver----=====================================
 * [..]
 * @note
 *   -#
 *
 * @warning
 *   -#
 *
 * *****************************************************************************************************
 * @attention
 *
 * This document is required and please understand any bugs that may occur in the document.
 * If you want to modify this file, after the code update is completed, update the version number in time,
 * indicate the update date, and fill in the changes
 * Make sure that the submitted code does not have many bugs.
 *
 * All rights reserved.
 * *****************************************************************************************************
 */

/* Includes Header C++--------------------------------------------------------*/
#include "serial_device.h"

/* Private typedef -----------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private Class -------------------------------------------------------------*/

using namespace std;
namespace ConnectDriver
{
#define DEFAULT_TIMEOUT (2000)
  static const uint8_t crc8_table[256] = {};
  Class_ConnectDriver::Class_ConnectDriver(void (*Serial_RxCall)(uint8_t command, uint8_t *buffer, size_t size))
  {
    this->_baudrate = 0;
    this->callback = Serial_RxCall;
  }

  bool Class_ConnectDriver::SetSerialPort(const std::string port_name, const uint32_t baudrate)
  {
    this->_port_name = port_name;
    this->_baudrate = baudrate;
  }
  bool Class_ConnectDriver::Connect()
  {
    try
    {
      // 设置端口
      connect.setPort(_port_name);
      // 设置波特率
      connect.setBaudrate(_baudrate);
      // 100ms超时
      serial::Timeout to = serial::Timeout::simpleTimeout(DEFAULT_TIMEOUT);
      connect.setTimeout(to);
      // 1位停止位
      connect.setStopbits(serial::stopbits_t::stopbits_one);
      // 8位数据位
      connect.setBytesize(serial::bytesize_t::eightbits);
      // 0校验位
      connect.setParity(serial::parity_t::parity_none);
      // 打开串口
      connect.open();
    }
    catch (serial::IOException &e)
    {
      std::cout << "unable to open ser" << std::endl;
      return false;
    }
    catch (std::invalid_argument &e)
    {
      std::cout << "std::invalid_argument" << std::endl;
      return false;
    }
    catch (serial::SerialException &e)
    {
      std::cout << "serial::SerialException" << std::endl;
      return false;
    }
    // 检查串口是否打开
    if (connect.isOpen())
    {
      std::cout << "ser open" << std::endl;
      return true;
    }
    else
    {
      return false;
    }
  }

  void Class_ConnectDriver::Disconnect()
  {
    connect.close();
  }

  void Class_ConnectDriver::Send(uint8_t *buffer, size_t size)
  {
    connect.write(buffer, size);
  }

  /**
   * @brief 此函数需要以100Hz运行
   *
   */
  void Class_ConnectDriver::Receive()
  {
    size_t size;
    if (connect.available())
    {
      size = connect.read(Rx_buffer, connect.available());
      this->callback(1, Rx_buffer, size);
    }
  }
}

/**
 * @}
 */

/**
 * @}
 */

/**
 * @}
 */

/*********************************END OF FILE*********************************/
